import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {RmRoleMenu} from '../entity/rm-role-menu';
import {RmRoleMenuSearchForm} from '../entity/rm-role-menu-search-form';
import {Observable} from 'rxjs';
import {baseHandleError, BaseService, ReturnForm} from '@sb/base';
import {catchError} from 'rxjs/operators';
import {RmMenuTreeNode} from '../../rm-menu/entity/rm-menu-tree-node';

/**
 * 功能服务
 */
@Injectable({
    providedIn: 'root'
})
export class RmRoleMenuService extends BaseService<RmRoleMenu, RmRoleMenuSearchForm> {

    basePath = 'rm/roleMenu';

    constructor(
        private http: HttpClient
    ) {
        super(http);
    }


    updateRoleMenu(roleId: string, menuIds: string[]): Observable<ReturnForm<string>> {
        return this.http.post<ReturnForm<string>>(`${this.url}/updateRoleMenu`, {
            roleId: roleId,
            menuIds: menuIds
        }).pipe(
            catchError(baseHandleError<ReturnForm<string>>('post'))
        );
    }

    getMenuNzTree(roleId: string): Observable<ReturnForm<RmMenuTreeNode[]>> {
        return this.http.post<ReturnForm<RmMenuTreeNode[]>>(`${this.url}/getMenuNzTree`, {roleId: roleId}).pipe(
            catchError(baseHandleError<ReturnForm<RmMenuTreeNode[]>>('getMenuNzTree'))
        );
    }

}
